package com.banner.lyl.dao.impl;

import com.banner.lyl.dao.IUserDao;
import com.banner.lyl.entity.User;
import com.banner.lyl.utils.DruidConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author: Re
 * @Date: 2021/3/20 21:28
 */
public class UserDaoImpl implements IUserDao {
    DruidConnectionUtils druidConnectionUtils = new DruidConnectionUtils();

    @Override
    public boolean insertUser(Connection connection, User user) {
        String iSql = "insert into userTest (userName,userPassword) values(?,?);";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(iSql);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getUserPwd());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            System.out.println("用户添加失败");
            e.printStackTrace();
            return false;
        }finally{
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                System.out.println("资源关闭失败");
                e.printStackTrace();
                return false;
            } finally {
                druidConnectionUtils.closeConnection(connection,preparedStatement, null);
                return true;
            }
        }
    }
}
